import { Component, OnInit, Input } from '@angular/core';

import { AlertController } from '@ionic/angular';

import { HttpService } from 'src/app/shared/service/http.service';

import { AppGlobalService } from '../../shared/service/app-global.service';
@Component({
    selector: 'app-counselor',
    templateUrl: './counselor.component.html',
    styleUrls: ['./counselor.component.scss']
})
export class CounselorComponent implements OnInit {

    @Input() tagData: any;
    showData: any;
    showList = true;
    defaultPhoto: string;
    appTypeCustom: any;

    constructor(
        private appGlobalService: AppGlobalService,
        private alertController: AlertController,
        private http: HttpService
    ) {
    }

    ngOnInit() {
        const bodyStyles = window.getComputedStyle(document.body);
        this.appTypeCustom = bodyStyles.getPropertyValue('--appTypeCustom').trim().replace('"', '').replace('"', '');
        this.defaultPhoto = 'assets/img/head.png';
        if (this.tagData.data.notloginData) {
            this.showList = true;
            this.showData = this.tagData.data.notloginData;
        } else if (this.tagData.data.loginedData) {
            this.showList = false;
            this.showData = this.tagData.data.loginedData;
            if (this.showData.area && this.showData.area.indexOf(",") !== -1) {
                this.showData.area = this.showData.area.replace(/,/g, '/').substring(0, this.showData.area.length - 1);
            }
            if (this.showData.area && this.showData.trade.indexOf(",") !== -1) {
                this.showData.trade = this.showData.trade.replace(/,/g, '/').substring(0, this.showData.trade.length - 1);
            }
        }
    }
    go(obj) {
        const pageParams = {
            pageId: obj.target,
            params: obj.params,
            sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(pageParams);
    }
    async call(mobile, name ) {
        const alert = await this.alertController.create({
            header: this.tagData.data.nick_name,
            message: name + " " + mobile,
            buttons: [
                {
                    text: '取消',
                    role: 'cancel',
                    cssClass: 'secondary'
                }, {
                    text: '拨打',
                    handler: () => {
                        this.http.changeCallNumber(mobile);
                    }
                }
            ]
        });
        await alert.present();
    }

    getNgStyle(str) {
        try {
            return JSON.parse(str);
        } catch (error) {
            return {};
        }
    }
}
